#!/bin/bash
set -e

# create opentap group if it doesn't exist
getent group opentap || groupadd opentap && echo "Created opentap group"

echo "Changing permissions of opentap folder (/usr/share/opentap)"
chgrp -R opentap /usr/share/opentap
# set sticky bit for the directory so new files will belong to the opentap group
chmod -R g+rwxs /usr/share/opentap

echo "opentap group configured."

# Attempt to add a user to the opentap group if a regular user installed opentap using sudo.
if [ ! -z "$SUDO_USER" ]; then
  if [ ! "$SUDO_USER" == "root" ]; then
    usermod -a -G opentap "$SUDO_USER"
    echo "Added user $SUDO_USER to the opentap group"
  fi
fi

echo "A user must be a member of the 'opentap' group to use opentap."
echo "Add users to the 'opentap' group with the following command:"
echo "    usermod -a -G opentap \$USER"
echo "This OpenTAP installation is global. If this is a multi-user system, consider creating a user-level OpenTAP
installation with"
echo "    tap package install OpenTAP --target /local/install/dir"
